package com.tark.service.qrcode.controller;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.tark.service.qrcode.pojo.QrcodePojo;
import com.tark.service.qrcode.service.QrcodeService;
import com.tark.service.qrcode.service.QrcodeServiceImp;
import com.tark.service.qrcode.util.checkPojo.checkParameter;
import com.tark.service.qrcode.util.model.ApiResult;
import com.tark.service.qrcode.util.model.utils.ApiResultUtils;



@RestController
@RequestMapping("/service")
public class QrcodeController {
	@Autowired
	private QrcodeService QrcodeService;
	
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@RequestMapping(value="/qrcode",method=RequestMethod.GET)
public ApiResult generateQrcode(QrcodePojo qrcodePojo){
		//校验思路 先判断参数是不是合法的 然后 单独判断有没有log
		qrcodePojo.setContent("langhaochen");
		qrcodePojo.setBarcodeFormat("CODE_128");
		qrcodePojo.setBasePath("F://新致方舟、");
		qrcodePojo.setFileName("zxing.jpg");
		qrcodePojo.setLength(300);
		qrcodePojo.setLogoFullPath("F://新致方舟//time.jpg");
		ApiResult apiResult = new ApiResult();
		
		try {
			boolean check = checkParameter.check(qrcodePojo);
			if(check){
				
				if (StringUtils.isBlank(qrcodePojo.getFileName())) {
				    String generateQrcode = QrcodeService.generateQrcode(qrcodePojo);
				    apiResult = ApiResultUtils.buildSuccessApiResult("创建成功", "service/qrcode", "生成地址", generateQrcode);
				    return apiResult;
				}else {
					//先创建没有LOGO的
					String imgPath=QrcodeService.generateQrcode(qrcodePojo);
					//插入LOGO的静态方法
					QrcodeServiceImp.generateQRImage(qrcodePojo, imgPath);
					apiResult=ApiResultUtils.buildSuccessApiResult("创建成功", "service/qrcode","生成地址" , imgPath);
					return apiResult;
				}
			}		
		} catch (Exception e) {
			apiResult=ApiResultUtils.buildFailedApiResult("创建失败", e.getMessage(), "service/qrcode", "异常", e);
			logger.error("创建失败", e);
			e.printStackTrace();
			return apiResult;
		}		
	return null;
	}
	
	
	
@RequestMapping(value="/qrcode/{fullFilePath}",method=RequestMethod.GET)
public ApiResult readQrcode(@PathVariable String fullFilePath){
		ApiResult apiResult = new ApiResult();
		try {
			fullFilePath="F://新致方舟//zxing.jpg";
			System.out.println(fullFilePath);
			if(StringUtils.isNotBlank(fullFilePath)){
			String readQrcode = QrcodeService.readQrcode(fullFilePath);	
			apiResult =ApiResultUtils.buildSuccessApiResult("解码成功", "service/qrcode", "内容为", readQrcode);
			return apiResult;
			}
		} catch (Exception e) {
			apiResult = ApiResultUtils.buildFailedApiResult("解码失败", e.getMessage(), "service/qrcode", "异常信息", e);
			logger.error("解码失败", e);
			e.printStackTrace();
			return apiResult;
		}
	return null;
	}
}
